﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace DBSyn
{
    public partial class frmSynDB : Form
    {
        //声明一个委托
        delegate void DelegateUpdateServerInfo(string str);//代理，更新界面消息
        Thread Loadthread = null;//下载数据线程
        public frmSynDB()
        {
            InitializeComponent();
        }

        #region 更新文本显示的内容
        /// <summary>
        /// 显示信息
        /// </summary>
        /// <param name="str"></param>
        private void UpdateServerMessage(string str)
        {
            if (InvokeRequired)
            {
                Invoke(new DelegateUpdateServerInfo(UpdateServerMessage), str);
            }
            else
            {
                if (str == "c")
                    rtbMsg.Clear();
                //显示相应信息
                rtbMsg.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + str + "\n");
                //自动滚动到最后一行
                rtbMsg.SelectionStart = rtbMsg.Text.Length;
                rtbMsg.ScrollToCaret();
            }
        }
        //记录消息
        public void DelegateSendRecord(object obj)
        {
            UpdateServerMessage(obj.ToString());
        }
        #endregion

        #region 下载数据
        /// <summary>
        /// 下载数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(p =>
            {
                Loadthread = new Thread(new ThreadStart(this.StarLoadListen));//初始化读取线程
                Loadthread.Start();
            });

        }
        private void StarLoadListen()
        {
            UpdateServerMessage("开始下载数据.......");
            #region 从退换坏卡测试数据系统下载读取数据
            //DataTable dt = DbHelperSQL.GetQueryResult(" SELECT * FROM JY_BISLPCARDACCTB ", true);
            DataTable dt = DbHelperSQL.GetQueryResult(" SELECT * FROM v_bicardacctb ", true);
            UpdateServerMessage("共查询到" + dt.Rows.Count + "条数据！");


            #endregion
        }
        #endregion

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void But_exit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认退出吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                Application.Exit();
        }

        /// <summary>
        /// 清除消息内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Clear_Click(object sender, EventArgs e)
        {
            UpdateServerMessage("c");
        }


    }
}
